let modalEvent = {
    distanceList: [0, 0],
    disPoint: { x: 0, y: 0 }
}
let status = 0 //判断时手指
var app = getApp()
var getList = function(that) {
    app.getApi.getProducts("POST", {
        pageSize: 0,
        method: "opg.eventTable.find",
        eventId: that.data.eventId
    }).then(data => {
        var list = that.data.items;
        if (data.data.totalCount == 0) {
            that.setData({
                noData: true
            })
            return;
        }
        for (let i = 0; i < data.data.result.length; i++) {
            data.data.result[i].location = JSON.parse(data.data.result[i].location)
            list.push(data.data.result[i])
        }
        if (Math.ceil(data.data.totalCount / 10) == that.data.pageNumber) {
            that.setData({
                isAll: true
            })
        }
        data.data.eventTableFile.itemsLocation = JSON.parse(data.data.eventTableFile.itemsLocation)
        that.setData({
            items: list,
            loadMore: false,
            eventTableFile: data.data.eventTableFile
        })
    })
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNumber: 1,
        isNotEndNow: 1,
        hidden: false, //没有更多
        loadMore: false, //是否显示加载更多
        isAll: false,
        items: [],
        noData: false,
        canvas_style: {
            style: 'width:100%',
            status: 0, //1代表放大 0代表缩小
            height: '750rpx'
        },
        w_scale: null,
        h_scale: null,
        length: 0,
        imageload: true,
        img: {
            left: 0,
            top: 0,
            baseScale: 1
        },
        is_model: false,
        loadMore: true,
        noImage: "../../images/noImage.png",
        model_item: {} //选座信息
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this
        that.setData({
            isChinese: wx.getStorageSync("isChinese")
        })
        wx.setNavigationBarTitle({
            title: that.data.isChinese ? "选桌" : "Table Map"
        })
        app.getApi.getProducts("POST", {
            id: options.id,
            method: "opg.eventPromote.get"
        }).then(data => {
            that.setData({
                eventPromote: data.data.eventPromote,
                eventId: data.data.eventPromote.eventId,
                id: data.data.eventPromote.id,
                eventType: data.data.eventPromote.eventType,
                venueName: app.globalData.venueName,
                eventName: app.globalData.eventName,
                cityName: app.globalData.cityName,
                eventLocation: app.globalData.location,
                loadMore: false
            })
            getList(that)
        })
        wx.getSystemInfo({
            success: function(res) {
                that.setData({ windowHeight: res.windowHeight })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReachBottom() {
        var that = this
        if (!that.data.isAll) {
            that.setData({
                pageNumber: that.data.pageNumber + 1,
                loadMore: true

            })
            getList(that)
        } else {
            that.setData({
                hidden: true
            })
        }
    },
    orderDetail(e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../orderDetails/orderDetails?id=' + id,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    pay(e) {
        var location = e.currentTarget.dataset.location,
            balance = e.currentTarget.dataset.balance;
        // if (balance == 0) {
        //     wx.showModal({
        //         title: this.data.isChinese ? '提示' : 'Prompt',
        //         showCancel: false,
        //         content: this.data.isChinese ? '已经没有剩余桌位' : 'Have no remaining table'
        //     })
        //     return;
        // }
        wx.navigateTo({
            url: '../chooseTable/chooseTable?eventId=' + this.data.eventId + "&id=" + this.data.id + "&location=" + location + "&isType=1",
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    imageload(e) {
        let w_scale = 375 / e.detail.width

        this.data.items.forEach((item, index) => {
            if (item.location.meta['_type'] == "ellipse") {
                item.location.meta.cy = w_scale * (item.location.meta.cy - item.location.meta.ry)
                item.location.meta.cx = w_scale * (item.location.meta.cx - item.location.meta.rx)
                item.location.meta.rx = w_scale * item.location.meta.rx * 2
                item.location.meta.ry = w_scale * item.location.meta.ry * 2
            } else {
                item.location.meta.width = w_scale * item.location.meta.width
                item.location.meta.height = w_scale * item.location.meta.height
                item.location.meta.x = item.location.meta.x * w_scale
                item.location.meta.y = item.location.meta.y * w_scale
            }
        })

        this.setData({
            items: this.data.items,
            w_scale: w_scale
        })
    },
    fitImage(w, h, type, tn) {
        // let n 
        // n = type ?w
        // return arr
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },




    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },

    touchMove(e) {
        this.setData({
            [`page_view`]: 'height:100%;overflow:hidden;'
        })
        if (e.touches.length == 2) {
            if (status == 1) {
                status = 2
                return false
            }
            var xMove = e.touches[1].clientX - e.touches[0].clientX
            var yMove = e.touches[1].clientY - e.touches[0].clientY
            var distance = Math.sqrt(xMove * xMove + yMove * yMove)
            modalEvent.distanceList.shift()
            modalEvent.distanceList.push(distance)
            if (modalEvent.distanceList[0] == 0) { return }
            var distanceDiff = modalEvent.distanceList[1] - modalEvent.distanceList[0] //两次touch之间, distance的变化. >0,放大图片.<0 缩小图片
            var baseScale = this.data.img.baseScale + 0.005 * distanceDiff
            if (baseScale > 0) {
                this.data.img.baseScale = baseScale
                var imgWidth = baseScale * parseInt(this.data.img.imgWidth)
                var imgHeight = baseScale * parseInt(this.data.img.imgHeight)
                this.setData({ img: this.data.img })
            } else {
                this.data.img.baseScale = 1
                this.setData({ img: this.data.img })
            }
            // var xMove = e.touches[1].clientX - e.touches[0].clientX
            // var yMove = e.touches[1].clientY - e.touches[0].clientY			
        } else {
            this.data.img.left = e.touches[0].clientX - modalEvent.disPoint.x
            this.data.img.top = e.touches[0].clientY - modalEvent.disPoint.y
            this.setData({ img: this.data.img })
        }
        // wx.stopPullDownRefresh()
    },
    touchStart(e) {
        modalEvent.distanceList = [0, 0] //回复初始值
        modalEvent.disPoint = { x: 0, y: 0 }
        if (e.touches.length == 2) {
            status = 2
        } else {
            status = 1
        }
        modalEvent.disPoint.x = e.touches[0].clientX - this.data.img.left
        modalEvent.disPoint.y = e.touches[0].clientY - this.data.img.top
    },
    touchEnd() {
        !this.data.canvas_style.status && this.setData({
            [`page_view`]: ''
        })
    },
    tapMax() {
        this.data.canvas_style.status = this.data.canvas_style.status ? 0 : 1
        this.data.canvas_style.height = this.data.canvas_style.status ? (this.data.windowHeight * 2) : 750
        this.setData({
            [`canvas_style`]: this.data.canvas_style,
            [`page_view`]: this.data.canvas_style.status ? 'height:' + this.data.windowHeight * 2 + 'rpx;overflow:hidden;' : ''
        })
    },
    showResizeModal(e) {
        this.setData({ is_model: true, model_item: e.currentTarget.dataset.item })
    },
    cancel() {
        this.setData({ is_model: false })
    },
    book(e) {

        wx.navigateTo({
            url: '../chooseTable/chooseTable?item=' + JSON.stringify(e.currentTarget.dataset.item)
        })
    }
})